import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
// 1. 引入你需要的组件
import Vant from 'vant';
// 2. 引入组件样式
import 'vant/lib/index.css';

import Footer from '@/components/footer.vue'

// 引入路由
import router from './routers/index'
import { useCookies } from 'vue3-cookies';

createApp(App)
    .use(Vant)
    .use(router)
    .component('Footer', Footer)
    .mount('#app')

router.beforeEach(async (to, from, next) => {
    let isLogin = to.meta.isLogin ?? false;
    const { cookies } = useCookies();
    if (isLogin) {
        const LoginUser = cookies.get('LoginUser') ?? ''
        if(!LoginUser){
            showFailToast('未登录，请先去登录')
            next('/business/base/login')
            return false;
        }
        let data ={
            id: LoginUser.id,
            mobile: LoginUser.mobile
        }
        
        let result = await Api.check(data);

        if(result.code = 0){
            cookies.remove('LoginUser')
            showFailToast('非法登录')
            next('/business/base/login')
        }else{
            cookies.set("LoginUser", result.data);
            next();
        }
    } else {
        next();
    }
})